package com.umfwechat.util;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.CommonConsts;
import com.umpay.sp.model.common.ResMessage;
import com.umpay.sp.model.saas.AgentPerm;
import com.umpay.sp.service.saas.remote.AgentPermServiceRemote;

/**
 * 获取当前业务员菜单权限
 * @author Administrator
 *
 */
public class PermissionUtil {
	
	private static Logger logger = LoggerFactory.getLogger(PermissionUtil.class);
	
	@Resource
	private static AgentPermServiceRemote agentPermServiceRemote;

	/**
	 * @param salesmanId业务员名称
	 * @param agentId代理商编号
	 * @param assistId登录账号
	 * @param type菜单类型(1:分润查询; 2:代理商商户查询; 3:对账文件获取)
	 * @return
	 */
	public static boolean getPermission(String salesmanId, String agentId, String assistId, Integer type){
		 com.umpay.sp.model.common.ReqMessage reqMess = ReqMessageUtil.makeSpReq();
		 reqMess.put(CommonConsts.SALESMANID, salesmanId);
		 reqMess.put(CommonConsts.AGENTID,agentId);
		 reqMess.put(CommonConsts.AGENTID,assistId);
		 ResMessage res = null;
		 try {
			logger.info("【获取代理商权限控制接口】输入参数：" + reqMess);
			res = agentPermServiceRemote.getAgentPermByPri(reqMess);
			logger.info("【获取代理商权限控制接口】输出参数：" + res);
		} catch (Exception e) {
			return false;
		}
		String retCode = res.getRetCode();
		if (!RetCode.SUCCESS.equals(retCode)) {
			return false;
		}
		AgentPerm agentPerm = (AgentPerm) res.getResMap().get("agentPerm");
		if (agentPerm == null) {
			return false;
		}
		if(type == 1){	//分润查询权限
			if("1".equals(agentPerm.getSearchProfitPerm())){
				return true;
			}
		}
		if(type == 2){	//代理商商户查询权限
			if("1".equals(agentPerm.getSearchStorePerm())){
				return true;
			}
		}
		if(type == 3){	//对账文件获取权限
			if("1".equals(agentPerm.getFetchBillDocPerm())){
				return true;
			}
		}
		
		return false;
	}
}
